package com.javabank.ws.resource;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.javabank.ws.dao.DAOFacade;
import com.javabank.ws.dao.impl.DAOFacadeImpl;
import com.javabank.ws.domain.Account;
import com.javabank.ws.domain.Transaction;
import com.javabank.ws.logic.Logic;

@WebService
public class JavabankResource {
	private DAOFacade facade;
	private Logic logic;
	
	public JavabankResource() {
		facade = new DAOFacadeImpl();
        logic = new Logic(facade);
	}
	
	@WebMethod
	public void openAccount(Account account) {
		logic.addAccount(account);
	}
	
	@WebMethod
	public Account getAccount(String reknummer) {
		Account account = logic.getAccountByRekeningnummer(Integer.parseInt(reknummer));
		return account;
	}
	
	@WebMethod
	public boolean transfer(float amount, String reknummerFrom, String reknummerTo) {
		return logic.transfer(amount, Integer.parseInt(reknummerFrom), Integer.parseInt(reknummerTo));
	}

	@WebMethod
	public void alterAccount(Account account) {
		logic.updateAccount(account);
	}
	
	@WebMethod
	public Transaction[] getTransactions(String reknummer, String datum) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Date date = new Date();
		
		try {
			date = dateFormat.parse(datum);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		// Als date formaat niet juist is, worden alle transacties opgehaald 
		return logic.getTransactions(Integer.parseInt(reknummer), date);
	}
	
	@WebMethod
	public Transaction getTransaction(String transid) {
		Transaction transaction = logic.getTransactionById(Integer.parseInt(transid));
		return transaction;
	}
}